/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.unicam.fishpass.prism;

/**
 * Calculates the arithmetic expression that are in the project the case listed
 * are : + , - , * , / , % .
 * 
 * @author Baiocco Andrea
 */
public class ArithmeticExpression {

	private ArithmeticExpression left = null;
	private ArithmeticExpression right = null;
	private char operand;
	private int num;
	private String var;

	/**
	 * @param left
	 * @param right
	 * @param operand
	 *            expresses the arithmetic Expression with the operand and left
	 *            and right part
	 */

	public ArithmeticExpression(ArithmeticExpression left,
			ArithmeticExpression right, char operand) {
		this.left = left;
		this.right = right;
		this.operand = operand;
	}

	/**
	 * @param num
	 */
	public ArithmeticExpression(int num) {
		this.num = num;
	}
	
	/**
	 * 
	 */
	public ArithmeticExpression(String var, char operand){
		this.var = var;
		this.operand = operand;
	}
	
	public ArithmeticExpression(String var, char operand, float init) {
		  this(var, operand);
		  this.num = (int) init;
		 }

	/**
	 * Calculate arithmetic expression. The division, as well as the rest of the
	 * division, are ALWAYS considered among INTEGER values (i.e. decimal part
	 * is always truncated).
	 * 
	 * @return
	 */

	int calculateExpression() {
		  if(operand == 'p')
		   return ++num;
		  if(operand == 'm')
		   return  --num;
		  if (right == null && left == null) // left and right
		   return num;
		  // else...
		  int valueLeft = left.calculateExpression();
		  int valueRight = right.calculateExpression();
		  switch (operand) {
		  case ('+'):
		   return valueLeft + valueRight;
		  case ('-'):
		   return valueLeft - valueRight;
		  case ('*'):
		   return valueLeft * valueRight;
		  case ('/'):
		   return valueLeft / valueRight;
		  case ('%'):
		   return valueLeft % valueRight;
		  }
		  return 0;
		 }
	@Override
	public String toString() {
		if (right == null && left == null)
			return Integer.toString(num);
		//
		return "(" + left.toString() + operand + right.toString() + ")";
	}
}